package com.aisi.backend.controller;

import com.aisi.backend.domain.RestBean;
import com.aisi.backend.domain.RestCode;
import com.aisi.backend.domain.vo.request.DetailsAccountSaveVo;
import com.aisi.backend.domain.vo.request.TopicCreateVo;
import com.aisi.backend.domain.vo.request.TopicUpdateVo;
import com.aisi.backend.domain.vo.response.TopicDetailVo;
import com.aisi.backend.domain.vo.response.TopicPreviewVo;
import com.aisi.backend.service.TopicService;
import com.aisi.backend.service.impl.TopicServiceImpl;
import com.aisi.backend.utils.WebUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

/**
 * @Author: shenjianZ
 * @Date: 2024/5/11 下午9:04
 * @Description:
 */

@WebServlet("/api/forum/*")
public class ForumController extends BaseController {
    private TopicService topicService= new TopicServiceImpl();

    public void createTopic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer userId = (Integer) request.getAttribute("userId");
        TopicCreateVo topicCreateVo = WebUtils.readJson(request, TopicCreateVo.class);
        boolean flag = topicService.createTopic(userId, topicCreateVo);
        if (flag) {
            RestBean restBean = RestBean.success(RestCode.SUCCESS);
            WebUtils.writeJson(response, restBean);
        } else {
            RestBean restBean = RestBean.failure(RestCode.FAILURE);
            WebUtils.writeJson(response, restBean);
        }
    }

    public  void listTopic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer type = Integer.parseInt(request.getParameter("type"));
        List<TopicPreviewVo> list = topicService.listTopic(page, type);
        WebUtils.writeJson(response, RestBean.success(RestCode.SUCCESS, list));
    }

    public  void listTopicDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer tid = Integer.parseInt(request.getParameter("tid"));
        TopicDetailVo vo = topicService.listTopicDetail(tid);
        if (vo != null) {
            
            WebUtils.writeJson(response, RestBean.success(RestCode.SUCCESS, vo));
        }
        else {
            WebUtils.writeJson(response, RestBean.failure(RestCode.DATA_NOT_FOUND));
        }
    }

    public void updateTopic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer userId = (Integer) request.getAttribute("userId");
        TopicUpdateVo topicUpdateVo = WebUtils.readJson(request, TopicUpdateVo.class);
        Integer i = topicService.updateTopic(userId, topicUpdateVo);
        if (i > 0) {
            RestBean restBean = RestBean.success(RestCode.SUCCESS);
            WebUtils.writeJson(response, restBean);
        } else {
            RestBean restBean = RestBean.failure(RestCode.FAILURE);
            WebUtils.writeJson(response, restBean);
        }
    }

}
